<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Hash;
use Mail;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests\AdminloginRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class HomeZhuceController extends Controller
{
    public function getZhuce()
    {
        return view('home.zhuce.zhuce');
    }
	
    //验证ajax用户名
    public function postAjax(Request $request)
    {
        //接收数据
        $name = $request->input('name');
        //查询
        $res = DB::table('user')->where('username',$name)->first();
        //判断
        if($res){
            echo 1;
        }else{
            echo 2;
        }
    }
    //注册
    public function postRegister(Request $request)
    {

        //判断验证码是否正确
        if($request->input('yanzhengma') != session('Vcode')){
            return back()->with('error','验证码不正确');
        }
        //接收数据
        $data = $request->only(['username','email','password']);
        //密码加密
        $data['password'] = Hash::make($data['password']);
        //状态
        $data['status'] = 1;//1未激活
        //权限
        $data['qx'] = 1;//1未激活
        //随机token
        $data['token'] = str_random(50);
        //注册时间
        $data['rtime'] = time();
        //注册 发邮件 传 id token
        $id = DB::table('user')->insertGetId($data);
        //判断
        if($id){
            //调用发送邮件
            $this->email($id,$data['token'],$data['email']);
            //跳转页面
            return view('home.email.success');
        }else{
            return back()->with('注册失败,请联系管理员');
        }
    }

    //验证码
    public function getVode()
    {
        ob_clean();//清除输出缓存区的内容
        //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        session(['Vcode'=>$phrase]);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();

       //看到图片输出 需要die
    }

    //邮箱
    public function email($id,$token,$email)
    {
        // Mail::raw('狗蛋网欢迎你注册请点击.即可获取大量资源...', function ($message) {
        //     //设置邮件标题
        //     $message->subject('激活邮件');
        //     //发送给谁
        //     $message->to('13839364829@163.com');
        // });
        Mail::send('home.email.re', ['id'=>$id,'token'=>$token], function ($message)use($email) {
        $message->to($email)->subject('激活邮件');
        });
    }

    //激活
    public function getJihuo(Request $request)
    {
        // 接收数据
        $id = $request->input('id');
        //查询
        $res = DB::table('user')->where('id',$id)->first();
        //判断token是否一样
        if($request->input('token') == $res->token){
            $data['status'] = 2;//激活
            $data['token'] = str_random(50);
            //修改
            $user = DB::table('user')->where('id',$id)->update($data);
            if($user){
                echo '成功激活';
                return redirect('/');
            }else{
                echo '激活失败';
            }
        }else{
            die('非法链接');
        }
    }

    //注册时 验证邮箱能用不
    public function postEmail(Request $request)
    {
        $res = DB::table('user')->where('email',$request->input('email'))->first();
        if($res){
            //不可一
            echo 2;
        }else{
            //可用
            echo 1;
        }
    }

    //判断输入的验证码是否正确
    public function postYanzheng(Request $request)
    {
        if($request->input('ma') != session('Vcode'))
        {
            echo 1;
        }else{
            echo 2;
        }    
    }
}
